package org.hamster.multidropbox.mainView.view
{
	import mx.collections.ArrayCollection;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.main.utils.PureMVCHelper;
	import org.hamster.multidropbox.mainView.event.AppFileEvent;
	
	public class FileListCollectionViewMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view";
		
		private var fileListViewMediatorList:ArrayCollection = new ArrayCollection();
		
		public function FileListCollectionViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			
		}
		
		override public function onRemove():void
		{
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppDropboxEvent.DROPBOX_CLIENT_CHANGE,
			];
		}
		
		public function handleDropboxClientChange(data:Object):void
		{
			var clientList:ArrayCollection = PureMVCHelper.dropboxClientListProxy.dropboxClientList;
			var addList:Array    = new Array();
			var removeList:Array = new Array();
			var mediator:FileListViewMediator;
			var client:AppDropboxClient;
			var contains:Boolean = false;
			for each (mediator in fileListViewMediatorList) {
				contains = false;
				for each (client in clientList) {
					if (mediator.uid == client.uid) {
						contains = true;
						break;
					}
				}
				if (!contains) {
					removeList.push(client);
				}
			}
			
			for each (client in clientList) {
				contains = false;
				for each (mediator in fileListViewMediatorList) {
					if (client.uid == mediator.uid) {
						contains = true;
						break;
					}
				}
				if (!contains) {
					addList.push(client);
				}
			}
			
			for each (client in addList) {
				this.createFileListView(client);
			}
			for each (client in removeList) {
				this.removeFileListView(client);
			}
		}
		
		private function createFileListView(client:AppDropboxClient):void
		{
			var newView:FileListView = new FileListView();
			var mediator:FileListViewMediator = new FileListViewMediator(client, newView);
			facade.registerMediator(mediator);
			this.app.mainGroup.addElement(newView);
			fileListViewMediatorList.addItem(mediator);
			
			var object:Object = {
				path : "",
				uid  : client.uid
			};
			mediator.sendNotification(AppFileEvent.FILE_PATH_CHANGE_REQUEST, object);
		}
		
		private function removeFileListView(client:AppDropboxClient):void
		{
			var mediator:FileListViewMediator = FileListViewMediator(facade.retrieveMediator(FileListViewMediator.NAME + "_" + client.uid));
			if (mediator != null) {
				this.app.mainGroup.removeElement(mediator.app);
				this.facade.removeMediator(mediator.getMediatorName());
				fileListViewMediatorList.removeItemAt(fileListViewMediatorList.getItemIndex(mediator));
			}
		}
		
		public function get app():FileListCollectionView
		{
			return viewComponent as FileListCollectionView;
		}
	}
}